package com.jwdf.uda.ui.adapter.viewholder;

import android.databinding.DataBindingUtil;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemQuizTakeVoteBinding;
import com.jwdf.uda.event.QuizDetailEvent;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.QuizDetailItem;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ToastUtils;

/**
 * Created by Administrator on 2016/9/20.
 */
public class QuizUserTakeVoteHolder extends AbsViewHolder<QuizDetailItem> {
    private static final int OPTION_C_TYPE = 3;
    private static final int OPTION_D_TYPE = 4;
    private ItemQuizTakeVoteBinding mBinding;
    private PbBean.PbSignBaseVoteOptionType optionId;

    public QuizUserTakeVoteHolder(View convertView) {
        super(convertView);
        mBinding = DataBindingUtil.bind(convertView);
        mBinding.tvOptionA.setOnClickListener(this);
        mBinding.tvOptionB.setOnClickListener(this);
        mBinding.tvOptionC.setOnClickListener(this);
        mBinding.tvOptionD.setOnClickListener(this);
        mBinding.btnTakeVote.setOnClickListener(this);
    }

    public PbBean.PbSignBaseVoteOptionType getOptionId() {
        return optionId;
    }

    public void setOptionId(PbBean.PbSignBaseVoteOptionType optionId) {
        this.optionId = optionId;
    }

    @Override
    protected void onBindView(QuizDetailItem item) {
        mBinding.llVote.setVisibility(item.voteOptionCount > 0 ? View.VISIBLE : View.GONE);
        mBinding.tvEmpty.setVisibility(item.voteOptionCount > 0 ? View.GONE : View.VISIBLE);
        switch (item.voteOptionCount) {
            case OPTION_C_TYPE:
                mBinding.flOptionC.setVisibility(View.VISIBLE);
                break;
            case OPTION_D_TYPE:
                mBinding.flOptionC.setVisibility(View.VISIBLE);
                mBinding.flOptionD.setVisibility(View.VISIBLE);
                break;
        }
        if (item.optionId != null) {
            item.optionId = null;
            setOptionId(item.optionId);
            mBinding.tvOptionA.setChecked(false);
            mBinding.tvOptionB.setChecked(false);
            mBinding.tvOptionC.setChecked(false);
            mBinding.tvOptionD.setChecked(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_option_a:
                mBinding.tvOptionA.toggle();
                mBinding.tvOptionB.setChecked(false);
                mBinding.tvOptionC.setChecked(false);
                mBinding.tvOptionD.setChecked(false);
                setOptionId(PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_A);
                break;
            case R.id.tv_option_b:
                mBinding.tvOptionB.toggle();
                mBinding.tvOptionA.setChecked(false);
                mBinding.tvOptionC.setChecked(false);
                mBinding.tvOptionD.setChecked(false);
                setOptionId(PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_B);
                break;
            case R.id.tv_option_c:
                mBinding.tvOptionC.toggle();
                mBinding.tvOptionA.setChecked(false);
                mBinding.tvOptionB.setChecked(false);
                mBinding.tvOptionD.setChecked(false);
                setOptionId(PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_C);
                break;
            case R.id.tv_option_d:
                mBinding.tvOptionD.toggle();
                mBinding.tvOptionA.setChecked(false);
                mBinding.tvOptionB.setChecked(false);
                mBinding.tvOptionC.setChecked(false);
                setOptionId(PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_D);
                break;
            case R.id.btn_take_vote:
                if (getOptionId() != null) {
                    QuizDetailEvent event = new QuizDetailEvent(QuizDetailEvent.EVENT_USER_TAKE_VOTE);
                    event.optionId = getOptionId();
                    EventBusUtil.post(event);
                } else {
                    ToastUtils.show(getConvertView().getContext(), "请先选择再点投票按钮~！");
                }
                break;
        }
    }
}
